package org.jeecg.modules.wmhb.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.DateUtil;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Calendar;
import cn.hutool.core.codec.Base64;
import java.awt.Color;
import java.awt.image.BufferedImage;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;

@Slf4j
public class WmhbUtil {

    /**
     * 计算两个日期间的天数差
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static Integer countDiffDay(Date startDate, Date endDate) {
        startDate = startDate != null ? startDate : new Date();
        endDate = endDate != null ? endDate : new Date();
        LocalDate localStartDate = startDate.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDate();
        LocalDate localEndDate = endDate.toInstant()
                .atZone(ZoneId.systemDefault())
                .toLocalDate();

        // 计算两个LocalDate之间的天数差异
        long daysBetween = ChronoUnit.DAYS.between(localStartDate, localEndDate);
        return (int) daysBetween + 1;
    }

    /**
     * 获取月份的第一天
     * @param date
     * @return
     */
    public static String getFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String formatDate = format.format(calendar.getTime());
        return formatDate;
    }

    /**
     * 获取月份的最后一天
     * @param date
     * @return
     */
    public static String getLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String formatDate = format.format(calendar.getTime());
        return formatDate;
    }

    /**
     * 将时间对象转成日期
     * @param date
     * @return
     */
    public static String convertDateToDatestring(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    /**
     * 将时间对象转成日期
     * @param date
     * @return
     */
    public static String convertDateToDatetime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    /**
     * 生成二维码
     */
    public static String generateQrCode(String url){
        // 配置二维码参数
        QrConfig config = new QrConfig(300, 300); // 宽高各300像素
        config.setMargin(2); // 边距
        config.setForeColor(Color.BLACK); // 前景色
        config.setBackColor(Color.WHITE); // 背景色

        // 生成二维码图片
        BufferedImage image = QrCodeUtil.generate(url, config);

        // 转换为 Base64 字符串（PNG 格式）
        return "data:image/png;base64," + Base64.encode(ImgUtil.toBytes(image, ImgUtil.IMAGE_TYPE_PNG));
    }
}